﻿<%@page import="store.pay.icbc.IcbcB2cPayObject"%>
<%@page import="store.pay.ConfigObjectHelper"%>
<%@page import="store.pay.icbc.IcbcB2cConfigObject"%>
<%@page import="store.pay.icbc.IcbcB2cPayResultObject"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="store.util.*" %>
<%@ page import="org.apache.http.StatusLine" %>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.HttpResponse" %>
<%@ page import="org.apache.http.NameValuePair" %>
<%@ page import="org.apache.http.client.entity.UrlEncodedFormEntity" %>
<%@ page import="org.apache.http.client.methods.HttpPost" %>
<%@ page import="org.apache.http.impl.client.DefaultHttpClient" %>
<%@ page import="org.apache.http.protocol.HTTP" %>
<%@ page import="org.apache.http.message.BasicNameValuePair" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="cn.com.infosec.icbc.ReturnValue" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String interfaceName = request.getParameter("interfaceName");
String interfaceVersion = request.getParameter("interfaceVersion");
String tranData = request.getParameter("tranData");
String merSignMsg = request.getParameter("merSignMsg");
String merCert = request.getParameter("merCert");

String bank_tranData=new String(ReturnValue.base64dec(tranData.getBytes()));

%>
<body>
<!--	<table width="100%" border="1">-->
<!--		<tr>-->
<!--			<td colspan="3">银行接受的数据信息：</td>-->
<!--		</tr>-->
<!--		<tr>-->
<!--			<td>interfaceName:</td>-->
<!--			<td colspan="2" ><%=interfaceName %></td>-->
<!--		</tr>-->
<!--		<tr>-->
<!--			<td>interfaceVersion:</td>-->
<!--			<td colspan="2"><%=interfaceVersion %></td>-->
<!--		</tr>-->
<!--		<tr>-->
<!--			<td>tranData:</td>-->
<!--			<td><%=tranData %></td>-->
<!--			<td><textarea rows="30" cols="50" ><%=bank_tranData %></textarea> </td>-->
<!--		</tr>-->
<!--		<tr>-->
<!--			<td>merCert:</td>-->
<!--			<td colspan="2"><%=merCert %></td>-->
<!--		</tr>-->
<!--		<tr>-->
<!--			<td>merSignMsg:</td>-->
<!--			<td colspan="2"><%=merSignMsg %></td>-->
<!--		</tr>-->
<!--	</table>-->
	<%
	byte[] byteSrc = bank_tranData.getBytes();
	byte[] DecSign = ReturnValue.base64dec(merSignMsg.getBytes());
    if (DecSign!=null){
    	out.println("<font face='Arial' size='4' color='Green'>签名信息BASE64解码成功</font><br>");
    	byte[] DecCert = ReturnValue.base64dec(merCert.getBytes());
    	if (DecCert!=null){
    		out.println("<font face='Arial' size='4' color='Green'>证书公钥BASE64解码成功</font><br>");
    		int a=ReturnValue.verifySign(byteSrc,byteSrc.length,DecCert,DecSign);
    		if (a==0) out.println("<font face='Arial' size='4' color='Green'>验签成功</font><br>");
    		else out.println("<font face='Arial' size='4' color='Red'>验签失败<br>验证返回码：</font><br>"+a);	    		
    	}else out.println("<font face='Arial' size='4' color='Red'>证书BASE64解码失败</font><br>");
    }else out.println("<font face='Arial' size='4' color='Red'>签名信息BASE64解码失败</font><br>");	
	
    SAXParserFactory spf = SAXParserFactory.newInstance();
	XmlParser xmlParser = new XmlParser();
	try {
		SAXParser saxParser = spf.newSAXParser();
		saxParser.parse(new ByteArrayInputStream(bank_tranData.getBytes()), xmlParser);
	} catch (Exception e) {
		e.printStackTrace();
	}
	IcbcB2cPayResultObject icbcB2cPayResultObject = xmlParser.getIcbcB2cPayResultObject();
	icbcB2cPayResultObject.setTranSerialNo("123456789");
	icbcB2cPayResultObject.setTranStat("1");
	icbcB2cPayResultObject.setComment("success");
	
	String notiyDataXml = icbcB2cPayResultObject.notifyDataXml();
	System.out.println(notiyDataXml);
	
	String merVAR="test";
	String notifyData = new String(ReturnValue.base64enc(notiyDataXml.getBytes()));
	String signMsg="";
	
	String password = "11111111";
	try{
		byte[] notidy_src = notiyDataXml.getBytes();
		char[] keyPass = password.toCharArray();
		
		byte[] bkey = FileUtil.file2Byte(
				FileUtil.getClasspath() + "/cert/icbc/b2c/user.key");
		

	    byte[] sign =ReturnValue.sign(notidy_src,notidy_src.length,bkey,keyPass);
	   
	    byte[] EncSign = ReturnValue.base64enc(sign);
	    signMsg=new String(EncSign).toString();
		    
	}catch (Exception e){
		out.println(e);
	}
	DefaultHttpClient httpclient = new DefaultHttpClient();
    try {

        HttpResponse httpresponse =null;
        HttpEntity entity = null;

        //HttpPost httpost = new HttpPost("http://chamdenjo.ip5.jspcn.net/payment/pay/notify/icbc/b2c/server");
        HttpPost httpost = new HttpPost("http://localhost/payment/pay/notify/icbc/b2c/server");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("merVAR", merVAR));
        nvps.add(new BasicNameValuePair("notifyData", notifyData));
        nvps.add(new BasicNameValuePair("signMsg", signMsg));

       // httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        httpresponse = httpclient.execute(httpost);
        entity = httpresponse.getEntity();
        StatusLine sl = httpresponse.getStatusLine();
        if(("OK").equals(sl.getReasonPhrase()) && 200==sl.getStatusCode()){
        	out.println("<font face='Arial' size='4' color='Green'>银行消息通知成功！</font><br>");
        	out.println("<font face='Arial' size='4' color='Green'>"+icbcB2cPayResultObject.getOrderid()+"支付成功！</font><br>");
        }else{
        	out.println("<font face='Arial' size='4' color='Red'>银行消息通知失败！</font><br>");
        }
        EntityUtils.consume(entity);

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
	%>
<!--	<form action="/payment/pay/notify/icbc/b2c/server" target="_new" method="POST">-->
<!--<table border="0" align="center">-->
<!--	<tr align="center">-->
<!--		<td colspan="3">订单信息</td>-->
<!--	</tr>-->
<!--	<tr>-->
<!--		<td>merVAR</td>-->
<!--		<td colspan="2"><input type="text"  name="merVAR" value="<%=merVAR %>"></input></td>-->
<!--	</tr>-->
<!--	<tr>-->
<!--		<td>notifyData</td>-->
<!--		<td colspan="2"><input type="text" name="notifyData" value="<%=notifyData %>"></input></td>-->
<!--	</tr>-->
<!--	<tr>-->
<!--		<td>signMsg</td>-->
<!--		<td colspan="2"><input type="text"  name="signMsg" value="<%=signMsg %>"></input></td>-->
<!--	</tr>-->
<!--	<tr>-->
<!--		<td align="center" colspan="3"><input type="submit" value="Submit"></input></td>-->
<!--	</tr>-->
<!--</table>-->
<!--</form>-->
</body>